package com.service.admin.product.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.service.admin.product.controller.req.ProductHrComboTypeAddReq;
import com.service.admin.product.controller.req.ProductHrComboTypePageReq;
import com.service.admin.product.service.ProductHrComboTypeService;
import com.service.core.exception.CommonException;
import com.service.core.rest.response.CommonPageRep;
import com.service.dao.mapper.CoinsMapper;
import com.service.dao.mapper.ProductHrComboMapper;
import com.service.dao.mapper.ProductHrComboTypeMapper;
import com.service.dao.model.Coins;
import com.service.dao.model.ProductHrCombo;
import com.service.dao.model.ProductHrComboType;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;

import java.util.Date;
import java.util.List;

/**
 * @author 404notfound
 */
@Service
public class ProductHrComboTypeServiceImpl implements ProductHrComboTypeService {

    @Autowired
    private ProductHrComboMapper productHrComboMapper;
    @Autowired
    private ProductHrComboTypeMapper productHrComboTypeMapper;

    @Autowired
    private CoinsMapper coinsMapper;

    @Override
    public CommonPageRep page(ProductHrComboTypePageReq pageReq) {
        Page<ProductHrCombo> page = PageHelper.startPage(pageReq.getPageNo(), pageReq.getPageSize());
        List<ProductHrComboType> productHrComboTypes = productHrComboMapper.selectProductHrComboTypeList(pageReq.getCoinName());
        return CommonPageRep.buildResultRep(page, productHrComboTypes);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void delete(Integer typeId) {

        Assert.notNull(typeId, "参数【typeId】不能为null");

        List<ProductHrCombo> productHrCombos = productHrComboMapper.selectList(new LambdaQueryWrapper<ProductHrCombo>().eq(ProductHrCombo::getComboType, typeId));
        if (!ObjectUtils.isEmpty(productHrCombos) && productHrCombos.size() > 0) {
            throw new CommonException("该类型已绑定产品，无法删除");
        }

        productHrComboMapper.deleteProductHrComboById(typeId);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void add(ProductHrComboTypeAddReq productHrComboTypeAddReq) {
        if (StringUtils.isEmpty(productHrComboTypeAddReq.getProductType())) {
            throw new CommonException("产品类型不能为空");
        }
        if (StringUtils.isEmpty(productHrComboTypeAddReq.getCoinName())) {
            throw new CommonException("币种名称");
        }

        Date now = new Date();
        Coins coins = coinsMapper.selectOne(new LambdaQueryWrapper<Coins>().eq(Coins::getCoinName, productHrComboTypeAddReq.getCoinName()));
        ProductHrComboType productHrComboType = new ProductHrComboType();
        productHrComboType.setCoinId(coins.getId());
        productHrComboType.setCoinName(coins.getCoinName());
        productHrComboType.setProductType(productHrComboTypeAddReq.getProductType());
        productHrComboType.setCreateTime(now);
        productHrComboType.setUpdateTime(now);
        productHrComboMapper.add(productHrComboType);
    }


    @Override
    public List<ProductHrComboType> list() {
        return productHrComboTypeMapper.selectList(null);
    }
}
